<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023/4/14
  Time: 10:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userName4search" id="userName4search"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
<%--                        <div class="layui-inline">--%>
<%--                            <label class="layui-form-label">手机号</label>--%>
<%--                            <div class="layui-input-inline">--%>
<%--                                <input type="text" name="phone" autocomplete="off" class="layui-input">--%>
<%--                            </div>--%>
<%--                        </div>--%>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="userTable" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

        // /**
        //  * 初始化表单，要加上，不然刷新部分组件可能会不加载
        //  */
        // table.render();

        table.render({
            elem: '#userTable',
            url: 'user/getUserList',
            method:'post',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'userId',align:'center', title: 'ID',sort: true},
                {field: 'nickName', title: '用户名'},
                {field: 'password', title: '密码', hide: true},
                {field: 'gender', title: '性别', sort: true,templet:function (data) {
                        if(data.gender === 1) {
                            return '男';
                        }else{
                            return '女';
                        }
                    }},
                {field: 'birthday',  title: '生日', sort: true,
                    templet:function (d) {
                        return layui.util.toDateString(d.birthday, 'yyyy-MM-dd');
                    }
                },
                {field: 'phone',  title: '手机号', sort: true},
                {field: 'email',  title: '邮箱', sort: true},
                {field: 'money', title: '余额', sort: true},
                {title: '操作', minWidth: 130, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var table=layui.table;
            var result = JSON.stringify(data.field);
            console.log(result);
            var search=$("#userName4search").val();
            console.log(search);
            //执行搜索重载
            table.reload('userTable', {
                url: 'user/getUserList',
                method:'post',
                where: {
                    search: search
                },
                page: {
                    curr: 1
                },
            });
            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var contentAdd = "/table/add"
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: contentAdd,
                    end: function (){
                        table.reload('userTable',{where: null})
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {
                var checkStatus = table.checkStatus('userTable')
                    , data = checkStatus.data;
                var userDel_idList = [];
                data.map(function(item){
                    userDel_idList.push({
                        userId: item.userId,
                        ask: "1"
                    });
                });
                layer.confirm('真的删除这些用户吗:\n'+JSON.stringify(userDel_idList), function (index){
                    //执行 Ajax 后重载
                    $.ajax({
                        url: '/user/userBatchDel',
                        method:'post',
                        data: JSON.stringify(userDel_idList),
                        dataType: "JSON",
                        contentType : 'application/json;charset=utf-8',
                        success:function(res){
                            if(res.status===200){
                                layer.msg(res.message,{icon:1,time:1000});
                                layer.close(index);
                                table.reload('userTable',{where: null})
                            }else{
                                layer.msg(res.message,{icon:0,time:1000});
                                layer.close(index);
                            }
                        },
                        error:function(e){
                            console.log(e,e.message);
                            alert('error');
                        }
                    });
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {

            var data = obj.data;
            if (obj.event === 'edit') {
                var contentEdit = "/table/edit?userId="+data.userId;
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: contentEdit,
                    success: function(layero, index){
                        // #aaa 表示的是弹出子页面的div的id值，
                        var div = layero.find('iframe').contents().find('#editFrom');
                        var body = layer.getChildFrame('body', index);
                        //获取子页面的index
                        var iframeWindow = window['layui-layer-iframe'+ index]
                        //给子页面id=aaa的dev里的不同ID值的元素赋值
                        div.find("#nickName").val(data.nickName);
                        div.find("#email").val(data.email);
                        div.find('#birthday').val(layui.util.toDateString(data.birthday, 'yyyy-MM-dd'));
                        div.find('#money').val(data.money);
                        div.find("input[name=gender][value='1']").attr("checked", data.gender == "1" ? true : false);
                        div.find("input[name=gender][value='0']").attr("checked", data.gender == "0" ? true : false);
                        form.render();
                    },
                    end: function (){
                        table.reload('userTable',{where: null})
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                var contentDel = "/user/deleteUser"
                layer.confirm('真的删除该行么', function (index) {
                    $.ajax({
                        url: contentDel,
                        data:JSON.stringify({
                            userId: data.userId,
                            ask: "1"
                        }),
                        contentType: "application/json;charset=UTF-8",
                        dataType: "json",
                        type: "POST",
                        success:function(res){
                            if(res.status===200){
                                layer.msg(res.message,{icon:1,time:1000});
                                layer.close(index);
                                table.reload('userTable',{where: null})
                            }else{
                                layer.msg(res.message,{icon:0,time:1000});
                                layer.close(index);
                            }
                        },
                        error:function(e){
                            console.log(e,e.message);
                            alert('error');
                        }
                    });
                });
            }
        });
    });
</script>
